import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-10-31
 * Time:10:57
 */
public class Test {
    //希尔排序
    public static void hillSort(int []array){
        int gap=array.length/2;
        while(gap>0){
           hill(array,gap);
            gap=gap/2;
        }
    }
    public static void hill(int []array,int gap){
        for (int i = gap; i < array.length; i++) {
            int j=i-gap;
            int tmp=array[i];
            for(; j >=0; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else {
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }

    //直接选择排序
    public static void intSort(int []array){
        for (int i = 0; i < array.length; i++) {
            int min=i;
            int j = i+1;
            for (; j < array.length; j++) {
                if(array[j]<array[min]){
                    min=j;
                }
            }
            int tmp=array[min];
            array[min]=array[i];
            array[i]=tmp;
        }
    }
    //插入排序优化
    public static void intSort1(int []array){
        for (int i = 0; i < array.length; i++) {
            int min=i;
            int max=i;
            int j = i+1;
            for (; j < array.length; j++) {
                if(array[j]<array[min]){
                    min=j;
                }
                if(array[j]>array[max]){
                    max=j;
                }
            }
            int tmpmin=array[min];
            array[min]=array[i];
            array[i]=tmpmin;
            if(max==i){
                max=min;
            }
            int tmpmax=array[max];
            array[max]=array[i];
            array[i]=tmpmax;
        }
    }
    //堆排序
    public static void heapSort(int[]array){
        //建立大根堆
        int parent=(array.length-1-1)/2;
        while (parent>0){
            shiftDown(array,parent,array.length-1);
            parent--;
        }
        //转换数组
        int end= array.length-1;
        while(end>0){
            int tmp=array[end];
            array[end]=array[0];
            array[0]=tmp;
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void shiftDown(int []array,int parent,int len){
        int child=parent*2+1;
        while(child<len){
            if(child+1< len&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                int tmp=array[child];
                array [child]=array[parent];
                array[parent]=tmp;
            }
            parent=child;
            child=parent*2+1;
        }
    }
    //冒泡排序
    public static void bubbleSort(int[]array){
        for (int i = 0; i < array.length; i++) {
            boolean flag=false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }else {
                    flag=true;
                }
            }
            if(flag){
                break;
            }
        }
    }
    //快速排序
    //和二叉树有关
    public static void quickSort(int []array){
        int start=0;
        int end=array.length-1;
        quick(array,start,end);
    }
    public static void quick(int []array,int start,int end){
        if(start>=end){
            return;
        }
        //找基准
        int mid=partition(array,start,end);
        quick(array,start,mid-1);
        quick(array,mid+1,end);
    }
    public static int partition(int []array,int left,int right){
        int i=left;
        int tmp=array[left];


        while (left<right){
            while(left<right&&array[right]>=tmp){
                right--;
            }
            while(left<right&&array[left]<=tmp){
                left++;
            }
            int ret=array[left];
            array[left]=array[right];
            array[right]=ret;
        }
        int ret=array[left];
        array[left]=array[i];
        array[i]=ret;
        return left;
    }
        public static void main(String[] args) {
        int []array={76,64,23,42,51,21,11};
//        intSort(array);
//        System.out.println(Arrays.toString(array));
//        heapSort(array);
//        bubbleSort(array);
        quickSort(array);
        System.out.println(Arrays.toString(array));
    }
}
